package com.spring.controller;

import com.spring.annotation.RequiredLog;
import com.spring.dao.ArticleDao;
import com.spring.dao.ArticleTagDao;
import com.spring.pojo.Article;
import com.spring.pojo.JsonResult;
import com.spring.pojo.Tag;
import com.spring.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private ArticleTagDao articleTagDao;

    @PostMapping
    public JsonResult doCreate(@RequestBody Article article,
                               HttpSession session){
         article.setCreatedTime(new Date());
         article.setModifiedTime(new Date());
         User user=(User)session.getAttribute("user");
         article.setUserId(user.getId());
         System.out.println("article="+article);
         articleDao.insert(article);
         articleTagDao.insert(article.getId(),article.getTagIds());
         return new JsonResult("create ok");
    }

    @RequiredLog(operation = "浏览文章")
    @GetMapping("/{id}")
    public JsonResult doSelectById(@PathVariable("id") Integer id){
        Article article = articleDao.selectById(id);
        return new JsonResult(article);
    }
}
